package com.relytosoft.mizar.media.common.emqx.client;

import com.relytosoft.mizar.media.common.emqx.callback.PushCallback;
import com.relytosoft.mizar.media.common.emqx.listener.*;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jierry
 * @version 2022/4/22.
 */
@Component
public class EmqxClient {

    private static final Logger log = LoggerFactory.getLogger(EmqxClient.class);

    @Resource
    private PushCallback pushCallback;
    @Resource
    private AlarmMqttMessageListener alarmMqttMessageListener;
    @Resource
    private CameraMqttMessageListener cameraMqttMessageListener;
    @Resource
    private DisplayMqttMessageListener displayMqttMessageListener;
    @Resource
    private StoreMqttMessageListener storeMqttMessageListener;
    @Resource
    private SystemMqttMessageListener systemMqttMessageListener;
    @Resource
    private FaceMqttMessageListener faceMqttMessageListener;
    @Resource
    private FaceDataMqttMessageListener faceDataMqttMessageListener;
    @Resource
    private StructureMqttMessageListener structureMqttMessageListener;
    @Resource
    private StruDataMqttMessageListener struDataMqttMessageListener;
    @Resource
    private PersonMqttMessageListener personMqttMessageListener;
    @Resource
    private PersonDataMqttMessageListener personDataMqttMessageListener;

    private static MqttClient client;

    public static void setClient(MqttClient client) {
        EmqxClient.client = client;
    }

    public static MqttClient getClient() {
        return client;
    }


    public void connect(String host, String clientID, String username, String password, int timeout, int keepalive, List<String> topicList) {
        MqttClient client;
        try {
            client = new MqttClient(host, clientID, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            if (username != null) {
                options.setUserName(username);
            }
            if (password != null) {
                options.setPassword(password.toCharArray());
            }
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepalive);
            EmqxClient.setClient(client);
            try {
                //设置回调类
                client.setCallback(pushCallback);
                //client.connect(options);
                IMqttToken iMqttToken = client.connectWithResult(options);
                boolean complete = iMqttToken.isComplete();
                log.info("MQTT连接"+(complete?"成功":"失败"));
                /** 订阅主题 **/
                for (String topic : topicList) {
                    log.info("连接订阅主题：{}", topic);
                    switch (topic){
                        case "mizar-media-system/#":
                            client.subscribe(topic, systemMqttMessageListener);
                            break;
                        case "mizar-media-camera/#":
                            client.subscribe(topic, cameraMqttMessageListener);
                            break;
                        case "mizar-media-alarm/#":
                            client.subscribe(topic, alarmMqttMessageListener);
                            break;
                        case "mizar-media-display/#":
                            client.subscribe(topic, displayMqttMessageListener);
                            break;
                        case "mizar-media-store/#":
                            client.subscribe(topic, storeMqttMessageListener);
                            break;
                        case "mizar-media-face/#":
                            client.subscribe(topic, faceMqttMessageListener);
                            break;
                        case "mizar-media-face-data/#":
                            client.subscribe(topic, faceDataMqttMessageListener);
                            break;
                        case "mizar-media-structure/#":
                            client.subscribe(topic, structureMqttMessageListener);
                            break;
                        case "mizar-media-structure-data/#":
                            client.subscribe(topic, struDataMqttMessageListener);
                            break;
                        case "mizar-media-person/#":
                            client.subscribe(topic, personMqttMessageListener);
                            break;
                        case "mizar-media-person-data/#":
                            client.subscribe(topic, personDataMqttMessageListener);
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
